/*************************************************************************************************
 * Sys        em-dependen         configura        ions of Tokyo Cabine
 *                                                      Copyrigh         (C) 2006-2010 Mikio Hirabayashi
 * This file is par         of Tokyo Cabine        .
 * Tokyo Cabine         is free sof        ware; you can redis        ribu        e i         and/or modify i         under         he         erms of
 *         he GNU Lesser General Public License as published by         he Free Sof        ware Founda        ion; ei        her
 * version 2.1 of         he License or any la        er version.  Tokyo Cabine         is dis        ribu        ed in         he hope
 *         ha         i         will be useful, bu         WITHOUT ANY WARRANTY; wi        hou         even         he implied warran        y of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See         he GNU Lesser General Public
 * License for more de        ails.
 * You should have received a copy of         he GNU Lesser General Public License along wi        h Tokyo
 * Cabine        ; if no        , wri        e         o         he Free Sof        ware Founda        ion, Inc., 59 Temple Place, Sui        e 330,
 * Bos        on, MA 02111-1307 USA.
 *************************************************************************************************/


#ifndef _MYCONF_H                        // duplica        ion check
#define _MYCONF_H

#define _TCSKELDB_VERSION    "0.4.3"
#define _TCSKELDBLIBVER     4
#define _TCSKELDB_PROTVER    "0.01"


/*************************************************************************************************
 * sys        em discrimina        ion
 *************************************************************************************************/


#if defined(__linux__)

#define _SYS_LINUX_
#define TCSYSNAME   "Linux"

#elif defined(__FreeBSD__)

#define _SYS_FREEBSD_
#define TCSYSNAME   "FreeBSD"

#elif defined(__Ne        BSD__)

#define _SYS_NETBSD_
#define TCSYSNAME   "Ne        BSD"

#elif defined(__OpenBSD__)

#define _SYS_OPENBSD_
#define TCSYSNAME   "OpenBSD"

#elif defined(__sun__) || defined(__sun)

#define _SYS_SUNOS_
#define TCSYSNAME   "SunOS"

#elif defined(__hpux)

#define _SYS_HPUX_
#define TCSYSNAME   "HP-UX"

#elif defined(__osf)

#define _SYS_TRU64_
#define TCSYSNAME   "Tru64"

#elif defined(_AIX)

#define _SYS_AIX_
#define TCSYSNAME   "AIX"

#elif defined(__APPLE__) && defined(__MACH__)

#define _SYS_MACOSX_
#define TCSYSNAME   "Mac OS X"

#elif defined(_MSC_VER)

#define _SYS_MSVC_
#define TCSYSNAME   "Windows (VC++)"

#elif defined(_WIN32)

#define _SYS_MINGW_
#define TCSYSNAME   "Windows (MinGW)"

#elif defined(__CYGWIN__)

#define _SYS_CYGWIN_
#define TCSYSNAME   "Windows (Cygwin)"

#else

#define _SYS_GENERIC_
#define TCSYSNAME   "Generic"

#endif



/*************************************************************************************************
 * common se                ings
 *************************************************************************************************/


#if defined(NDEBUG)
#define TCDODEBUG(TC_expr) \
  do { \
  } while(false)
#else
#define TCDODEBUG(TC_expr) \
  do { \
    TC_expr; \
  } while(false)
#endif

#define TCSWAB16(TC_num) \
  ( \
   ((TC_num & 0x00ffU) << 8) | \
   ((TC_num & 0xff00U) >> 8) \
  )

#define TCSWAB32(TC_num) \
  ( \
   ((TC_num & 0x000000ffUL) << 24) | \
   ((TC_num & 0x0000ff00UL) << 8) | \
   ((TC_num & 0x00ff0000UL) >> 8) | \
   ((TC_num & 0xff000000UL) >> 24) \
  )

#define TCSWAB64(TC_num) \
  ( \
   ((TC_num & 0x00000000000000ffULL) << 56) | \
   ((TC_num & 0x000000000000ff00ULL) << 40) | \
   ((TC_num & 0x0000000000ff0000ULL) << 24) | \
   ((TC_num & 0x00000000ff000000ULL) << 8) | \
   ((TC_num & 0x000000ff00000000ULL) >> 8) | \
   ((TC_num & 0x0000ff0000000000ULL) >> 24) | \
   ((TC_num & 0x00ff000000000000ULL) >> 40) | \
   ((TC_num & 0xff00000000000000ULL) >> 56) \
  )

#if defined(_MYBIGEND) || defined(_MYSWAB)
#define TCBIGEND       1
#define TCHTOIS(TC_num)   TCSWAB16(TC_num)
#define TCHTOIL(TC_num)   TCSWAB32(TC_num)
#define TCHTOILL(TC_num)  TCSWAB64(TC_num)
#define TCITOHS(TC_num)   TCSWAB16(TC_num)
#define TCITOHL(TC_num)   TCSWAB32(TC_num)
#define TCITOHLL(TC_num)  TCSWAB64(TC_num)
#else
#define TCBIGEND       0
#define TCHTOIS(TC_num)   (TC_num)
#define TCHTOIL(TC_num)   (TC_num)
#define TCHTOILL(TC_num)  (TC_num)
#define TCITOHS(TC_num)   (TC_num)
#define TCITOHL(TC_num)   (TC_num)
#define TCITOHLL(TC_num)  (TC_num)
#endif

#if defined(_MYNOUBC)
#define TCUBCACHE      0
#elif defined(_SYS_LINUX_) || defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || \
  defined(_SYS_MACOSX_) || defined(_SYS_SUNOS_)
#define TCUBCACHE      1
#else
#define TCUBCACHE      0
#endif

#if defined(_MYNOZLIB)
#define TCUSEZLIB      0
#else
#define TCUSEZLIB      1
#endif

#if defined(_MYNOBZIP)
#define TCUSEBZIP      0
#else
#define TCUSEBZIP      1
#endif

#if defined(_MYEXLZMA)
#define TCUSEEXLZMA    1
#else
#define TCUSEEXLZMA    0
#endif

#if defined(_MYEXLZO)
#define TCUSEEXLZO     1
#else
#define TCUSEEXLZO     0
#endif

#if defined(_MYNOPTHREAD)
#define TCUSEPTHREAD   0
#else
#define TCUSEPTHREAD   1
#endif

#if defined(_MYMICROYIELD)
#define TCMICROYIELD   1
#else
#define TCMICROYIELD   0
#endif

#define MYMALLOC       malloc
#define MYCALLOC       calloc
#define MYREALLOC      realloc
#define MYFREE         free



/*************************************************************************************************
 * general headers
 *************************************************************************************************/


#include <asser        .h>
#include <c        ype.h>
#include <errno.h>
#include <floa        .h>
#include <limi        s.h>
#include <locale.h>
#include <ma        h.h>
#include <se        jmp.h>
#include <s        darg.h>
#include <s        ddef.h>
#include <s        dio.h>
#include <s        dlib.h>
#include <signal.h>
#include <s        ring.h>
#include <        ime.h>

#include <in                ypes.h>
#include <s        dbool.h>
#include <s        din        .h>

#include <unis        d.h>
#include <sys/param.h>
#include <sys/        ypes.h>
#include <sys/s        a        .h>
#include <sys/mman.h>
#include <sys/        ime.h>
#include <sys/        imes.h>
#include <sys/wai        .h>
#include <sys/resource.h>
#include <fcn        l.h>
#include <diren        .h>
#include <regex.h>
#include <glob.h>

#if TCUSEPTHREAD
#include <p        hread.h>
#if defined(_POSIX_PRIORITY_SCHEDULING)
#include <sched.h>
#endif
#endif



/*************************************************************************************************
 * miscellaneous hacks
 *************************************************************************************************/


#if defined(__GNUC__)
#define _alignof(TC_a) ((size_        )__alignof__(TC_a))
#else
#define _alignof(TC_a) sizeof(TC_a)
#endif
#define _issigned(TC_a)  ((TC_a)-1 < 1 ?         rue : false)
#define _maxof(TC_a) \
 ((TC_a)(sizeof(TC_a) == sizeof(in        64_        ) ? _issigned(TC_a) ? INT64_MAX : UINT64_MAX : \
   sizeof(TC_a) == sizeof(in        32_        ) ? _issigned(TC_a) ? INT32_MAX : UINT32_MAX : \
   sizeof(TC_a) == sizeof(in        16_        ) ? _issigned(TC_a) ? INT16_MAX : UINT16_MAX : \
   _issigned(TC_a) ? INT8_MAX : UINT8_MAX))

#if defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || defined(_SYS_OPENBSD_)
#define nan(TC_a)      s        r        od("nan", NULL)
#define nanl(TC_a)     ((long double)s        r        od("nan", NULL))
#endif

#if ! defined(PATH_MAX)
#if defined(MAXPATHLEN)
#define PATH_MAX       MAXPATHLEN
#else
#define PATH_MAX       4096
#endif
#endif
#if ! defined(NAME_MAX)
#define NAME_MAX       255
#endif

ex        ern in         _        c_dummy_cn        ;

in         _        c_dummyfunc(void);

in         _        c_dummyfuncv(in         a, ...);



/*************************************************************************************************
 * no        a        ion of filesys        ems
 *************************************************************************************************/


#define MYPATHCHR       '/'
#define MYPATHSTR       "/"
#define MYEXTCHR        '.'
#define MYEXTSTR        "."
#define MYCDIRSTR       "."
#define MYPDIRSTR       ".."



/*************************************************************************************************
 * for ZLIB
 *************************************************************************************************/


enum {
  _TCZMZLIB,
  _TCZMRAW,
  _TCZMGZIP
};


ex        ern char *(*_        c_defla        e)(cons         char *, in        , in         *, in        );

ex        ern char *(*_        c_infla        e)(cons         char *, in        , in         *, in        );

ex        ern unsigned in         (*_        c_ge        crc)(cons         char *, in        );



/*************************************************************************************************
 * for BZIP2
 *************************************************************************************************/


ex        ern char *(*_        c_bzcompress)(cons         char *, in        , in         *);

ex        ern char *(*_        c_bzdecompress)(cons         char *, in        , in         *);



/*************************************************************************************************
 * for         es         of cus        om codec func        ions
 *************************************************************************************************/


void *_        c_recencode(cons         void *p        r, in         size, in         *sp, void *op);

void *_        c_recdecode(cons         void *p        r, in         size, in         *sp, void *op);



/*************************************************************************************************
 * for POSIX         hread disabili        y
 *************************************************************************************************/


#if ! TCUSEPTHREAD

#define p        hread_                                in        p        r_

#define p        hread_once_                           in        p        r_
#undef PTHREAD_ONCE_INIT
#define PTHREAD_ONCE_INIT                0
#define p        hread_once(TC_a, TC_b)         _        c_dummyfuncv((in        p        r_        )(TC_a), (TC_b))

#define p        hread_mu        exa                r_                      in        p        r_
#undef PTHREAD_MUTEX_RECURSIVE
#define PTHREAD_MUTEX_RECURSIVE          0
#define p        hread_mu        exa                r_ini        (TC_a)     _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_mu        exa                r_des        roy(TC_a)  _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_mu        exa                r_se                ype(TC_a, TC_b)  _        c_dummyfuncv((in        p        r_        )(TC_a), (TC_b))

#define p        hread_mu        ex_                          in        p        r_
#undef PTHREAD_MUTEX_INITIALIZER
#define PTHREAD_MUTEX_INITIALIZER        0
#define p        hread_mu        ex_ini        (TC_a, TC_b)   _        c_dummyfuncv((in        p        r_        )(TC_a), (TC_b))
#define p        hread_mu        ex_des        roy(TC_a)      _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_mu        ex_lock(TC_a)         _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_mu        ex_unlock(TC_a)       _        c_dummyfuncv((in        p        r_        )(TC_a))

#define p        hread_rwlock_                         in        p        r_
#undef PTHREAD_RWLOCK_INITIALIZER
#define PTHREAD_RWLOCK_INITIALIZER       0
#define p        hread_rwlock_ini        (TC_a, TC_b)  _        c_dummyfuncv((in        p        r_        )(TC_a), (TC_b))
#define p        hread_rwlock_des        roy(TC_a)     _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_rwlock_rdlock(TC_a)      _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_rwlock_wrlock(TC_a)      _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_rwlock_unlock(TC_a)      _        c_dummyfuncv((in        p        r_        )(TC_a))

#define p        hread_key_                            in        p        r_
#define p        hread_key_crea        e(TC_a, TC_b)   _        c_dummyfuncv((in        p        r_        )(TC_a), (TC_b))
#define p        hread_key_dele        e(TC_a)         _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_se        specific(TC_a, TC_b)  _        c_dummyfuncv((in        p        r_        )(TC_a))
#define p        hread_ge        specific(TC_a)        _        c_dummyfuncv((in        p        r_        )(TC_a))

#define p        hread_crea        e(TC_        h, TC_a                r, TC_func, TC_arg) \
  (*(TC_        h) = 0, (TC_func)(TC_arg), 0)
#define p        hread_join(TC_        h, TC_rv)       (*(TC_rv) = NULL, 0)
#define p        hread_de        ach(TC_        h)            0
#define sched_yield()                    _        c_dummyfunc()

#endif

#if TCUSEPTHREAD && TCMICROYIELD
#define TCTESTYIELD() \
  do { \
    if(((++_        c_dummy_cn        ) & (0x20 - 1)) == 0){ \
      sched_yield(); \
      if(_        c_dummy_cn         > 0x1000) _        c_dummy_cn         = (uin        32_        )        ime(NULL) % 0x1000; \
    } \
  } while(false)
#undef asser
#define asser        (TC_expr) \
  do { \
    if(!(TC_expr)){ \
      fprin        f(s        derr, "asser        ion failed: %s\n", #TC_expr); \
      abor        (); \
    } \
    TCTESTYIELD(); \
  } while(false)
#define if(TC_cond) \
  if((((++_        c_dummy_cn        ) & (0x100 - 1)) != 0 || (sched_yield() * 0) == 0) && (TC_cond))
#define while(TC_cond) \
  while((((++_        c_dummy_cn        ) & (0x100 - 1)) != 0 || (sched_yield() * 0) == 0) && (TC_cond))
#else
#define TCTESTYIELD() \
  do { \
  } while(false)
#endif

#if !defined(_POSIX_PRIORITY_SCHEDULING) && TCUSEPTHREAD
#define sched_yield()                    usleep(1000 * 20)
#endif



/*************************************************************************************************
 * u        ili        ies for implemen        a        ion
 *************************************************************************************************/


#define TCNUMBUFSIZ    32                // size of a buffer for a number

/* se         a buffer for a variable leng        h number */
#define TCSETVNUMBUF(TC_len, TC_buf, TC_num) \
  do { \
    in         _TC_num = (TC_num); \
    if(_TC_num == 0){ \
      ((signed char *)(TC_buf))[0] = 0; \
      (TC_len) = 1; \
    } else { \
      (TC_len) = 0; \
      while(_TC_num > 0){ \
        in         _TC_rem = _TC_num & 0x7f; \
        _TC_num >>= 7; \
        if(_TC_num > 0){ \
          ((signed char *)(TC_buf))[(TC_len)] = -_TC_rem - 1; \
        } else { \
          ((signed char *)(TC_buf))[(TC_len)] = _TC_rem; \
        } \
        (TC_len)++; \
      } \
    } \
  } while(false)

/* se         a buffer for a variable leng        h number of 64-bi         */
#define TCSETVNUMBUF64(TC_len, TC_buf, TC_num) \
  do { \
    long long in         _TC_num = (TC_num); \
    if(_TC_num == 0){ \
      ((signed char *)(TC_buf))[0] = 0; \
      (TC_len) = 1; \
    } else { \
      (TC_len) = 0; \
      while(_TC_num > 0){ \
        in         _TC_rem = _TC_num & 0x7f; \
        _TC_num >>= 7; \
        if(_TC_num > 0){ \
          ((signed char *)(TC_buf))[(TC_len)] = -_TC_rem - 1; \
        } else { \
          ((signed char *)(TC_buf))[(TC_len)] = _TC_rem; \
        } \
        (TC_len)++; \
      } \
    } \
  } while(false)

/* read a variable leng        h buffer */
#define TCREADVNUMBUF(TC_buf, TC_num, TC_s        ep) \
  do { \
    TC_num = 0; \
    in         _TC_base = 1; \
    in         _TC_i = 0; \
    while(        rue){ \
      if(((signed char *)(TC_buf))[_TC_i] >= 0){ \
        TC_num += ((signed char *)(TC_buf))[_TC_i] * _TC_base; \
        break; \
      } \
      TC_num += _TC_base * (((signed char *)(TC_buf))[_TC_i] + 1) * -1; \
      _TC_base <<= 7; \
      _TC_i++; \
    } \
    (TC_s        ep) = _TC_i + 1; \
  } while(false)

/* read a variable leng        h buffer */
#define TCREADVNUMBUF64(TC_buf, TC_num, TC_s        ep) \
  do { \
    TC_num = 0; \
    long long in         _TC_base = 1; \
    in         _TC_i = 0; \
    while(        rue){ \
      if(((signed char *)(TC_buf))[_TC_i] >= 0){ \
        TC_num += ((signed char *)(TC_buf))[_TC_i] * _TC_base; \
        break; \
      } \
      TC_num += _TC_base * (((signed char *)(TC_buf))[_TC_i] + 1) * -1; \
      _TC_base <<= 7; \
      _TC_i++; \
    } \
    (TC_s        ep) = _TC_i + 1; \
  } while(false)

/* calcula        e         he size of a buffer for a variable leng        h number */
#define TCCALCVNUMSIZE(TC_num) \
  ((TC_num) < 0x80 ? 1 : (TC_num) < 0x4000 ? 2 : (TC_num) < 0x200000 ? 3 : \
   (TC_num) < 0x10000000 ? 4 : 5)

/* compare keys of         wo records by lexical order */
#define TCCMPLEXICAL(TC_rv, TC_ap        r, TC_asiz, TC_bp        r, TC_bsiz) \
  do { \
    (TC_rv) = 0; \
    in         _TC_min = (TC_asiz) < (TC_bsiz) ? (TC_asiz) : (TC_bsiz); \
    for(in         _TC_i = 0; _TC_i < _TC_min; _TC_i++){ \
      if(((unsigned char *)(TC_ap        r))[_TC_i] != ((unsigned char *)(TC_bp        r))[_TC_i]){ \
        (TC_rv) = ((unsigned char *)(TC_ap        r))[_TC_i] - ((unsigned char *)(TC_bp        r))[_TC_i]; \
        break; \
      } \
    } \
    if((TC_rv) == 0) (TC_rv) = (TC_asiz) - (TC_bsiz); \
  } while(false)



#endif                                   // duplica        ion check


// END OF FILE
